package com.woniuxy.userapp.app.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.woniuxy.commons.util.Result;
import com.woniuxy.commons.util.WebUtil;
import com.woniuxy.userapp.client.CoachClient;
import com.woniuxy.userapp.client.param.AddCoachInfo;
import com.woniuxy.userapp.client.param.CoachInfo;
import com.woniuxy.userapp.client.param.StudentInfo;
import com.woniuxy.userapp.common.exception.Myexception.UserException;
import com.woniuxy.userapp.common.exception.code.UserExceptionCode;
import com.woniuxy.userapp.infra.model.mapper.model.Coach;
import com.woniuxy.userapp.infra.model.mapper.model.FrontAccount;
import com.woniuxy.userapp.infra.model.mapper.model.Student;
import com.woniuxy.userapp.service.CoachService;
import com.woniuxy.userapp.service.FrontAccountService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.*;


/**
 * <p>
 *  教练表
 * </p>
 *
 * @author 
 * @since 2022-08-03
 */
@RestController
public class CoachController implements CoachClient {

    @Autowired
    private CoachService coachService;
    @Autowired
    private CoachClient coachClient;
    @Autowired
    private FrontAccountService frontAccountService;

    BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();


    public Result<Integer> findAccountIdByCoachId(Integer CoachId){
        Coach coach = coachService.getOne(new QueryWrapper<Coach>().eq("id", CoachId));
        if(coach == null) throw new UserException(UserExceptionCode.COACH_NOT_EXISTST);
        return Result.getInstance(Integer.class).setCode(200).setData(coach.getFrontAccountId());
    }

    @Override
    public Result<CoachInfo> findCoachByAccountId(Integer accountId) {
        Coach coach = coachService.getOne(new QueryWrapper<Coach>().eq("front_account_id", accountId));
        if(coach == null) throw new UserException(UserExceptionCode.COACH_NOT_EXISTST);
        CoachInfo coachInfo = BeanUtil.copyProperties(coach, CoachInfo.class);
        return Result.getInstance(CoachInfo.class).setCode(200).setMsg("查询成功").setData(coachInfo);

    }

    @Override
    public Result<CoachInfo> findCoachByCoachId(Integer coachId) {
        Coach coach = coachService.getOne(new QueryWrapper<Coach>().eq("id", coachId));
        if(coach == null) throw new UserException(UserExceptionCode.COACH_NOT_EXISTST);
        CoachInfo coachInfo = BeanUtil.copyProperties(coach, CoachInfo.class);
        return Result.getInstance(CoachInfo.class).setCode(200).setCode(200).setData(coachInfo);
    }

    /**
     * 完善教练信息
     * @param addCoachInfo
     * @return
     */
    @Override
    public Result addCoach(AddCoachInfo addCoachInfo) {
        String header = WebUtil.getRequest().getHeader("user");
        JSONObject entries = new JSONObject(header);
        Integer account_id = Integer.valueOf(entries.getStr("LoginId"));
        Result<CoachInfo> coachResult = coachClient.findCoachByAccountId(account_id);
        //判断该学员是否存在
        if(coachResult.getData() == null) throw new UserException(UserExceptionCode.COACH_NOT_EXISTST);
        //先在账户表中查询是否有此qq.email,phone,再更新账户表中的数据,最后跟心教练表
        //判断电话是否重复
        String phone = addCoachInfo.getPhone();
        FrontAccount frontAccount = frontAccountService.getOne(new QueryWrapper<FrontAccount>().eq("phone", phone));
        if(frontAccount != null){
            if(frontAccount.getId() != account_id)throw new UserException(UserExceptionCode.PHONE_IS_EXISTS);
        }
//        if(frontAccount != null)throw new UserException(UserExceptionCode.PHONE_IS_EXISTS);
        //判断邮箱是否重复
        String email = addCoachInfo.getEmail();
        FrontAccount frontAccount1 = frontAccountService.getOne(new QueryWrapper<FrontAccount>().eq("email", email));
        if(frontAccount1 != null){
            if(frontAccount1.getId() != account_id)throw new UserException(UserExceptionCode.EMAIL_IS_EXISTS);
        }
//        if(frontAccount1 != null) throw new UserException(UserExceptionCode.EMAIL_IS_EXISTS);
        //判断qq号是否重复
        String qq = addCoachInfo.getQq();
        FrontAccount frontAccount2 = frontAccountService.getOne(new QueryWrapper<FrontAccount>().eq("qq", qq));
        if(frontAccount2 != null){
            if(frontAccount2.getId() != account_id) throw new UserException(UserExceptionCode.QQ_IS_EXISTS);
        }
//        if(frontAccount2 != null) throw new UserException(UserExceptionCode.QQ_IS_EXISTS);

        //账户表更新
        FrontAccount updateFrontAccount = new FrontAccount();
        updateFrontAccount.setId(account_id);
        updateFrontAccount.setQq(qq);
        updateFrontAccount.setEmail(email);
        updateFrontAccount.setPhone(phone);
        updateFrontAccount.setWalletPwd(encoder.encode(addCoachInfo.getWalletPwd()));
        frontAccountService.saveOrUpdate(updateFrontAccount,new QueryWrapper<FrontAccount>().eq("id",account_id));

        //更新教练表
        addCoachInfo.setFrontAccountId(account_id);
        Coach coach = BeanUtil.copyProperties(addCoachInfo, Coach.class);
        boolean save = coachService.saveOrUpdate(coach, new UpdateWrapper<Coach>().eq("front_account_id", account_id));
        if(!save) throw new UserException(UserExceptionCode.COACH_ADD_FAILS);
        return Result.getInstance().setMsg("新增教练信息成功").setCode(200);
    }
}

